package com.example.coupon.service;

import com.example.coupon.comon.enums.CouponStatusEnum;
import com.example.coupon.comon.enums.CouponTypeEnum;
import com.example.coupon.comon.enums.UserCouponStatusEnum;
import com.example.coupon.dao.CouponMapper;
import com.example.coupon.dao.UserCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Date;

@Service
public class CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private UserCouponMapper userCouponMapper;

    public Integer addCoupon(Long activityId, String amount, String startTime, String endTime) {
        return couponMapper.insertCoupon(activityId, amount, CouponTypeEnum.LI_JIAN.getCode(),
                CouponStatusEnum.NOT_START.getCode(), startTime, endTime);
    }

    public String obtainCoupon(Long userId, Long couponId) {
        Map<String, Object> couponMap = couponMapper.queryCouponById(couponId);
        if (couponMap.size() <= 0) {
            return "优惠券不存在，领取失败";
        }
        Integer status = Integer.parseInt(couponMap.get("status").toString());
        if (!CouponStatusEnum.UNDERWAY.getCode().equals(status)) {
            return "优惠券不在进行中，领取失败";
        }
        Integer result = userCouponMapper.insertUserCoupon(userId, couponId, UserCouponStatusEnum.NOT_USE.getCode(), new Date());
        if (result > 0) {
            return "用户领取成功";
        }
        return "用户领取失败";
    }

}